package com.xiaowu.netmusic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaowu.netmusic.common.constant.MessageConstant;
import com.xiaowu.netmusic.common.exception.UserNotLoginException;
import com.xiaowu.netmusic.domain.dto.UserCreateDTO;
import com.xiaowu.netmusic.domain.po.UserPO;
import com.xiaowu.netmusic.domain.vo.UserVO;
import com.xiaowu.netmusic.mapper.UserMapper;
import com.xiaowu.netmusic.service.UserService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

/**
 * @description:
 * @author: xiaowu
 * @time: 2025/4/27 20:34
 */
@Service
@AllArgsConstructor
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, UserPO> implements UserService {


    private final UserMapper userMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertUser(UserCreateDTO userCreateDTO) {

        if(userCreateDTO == null){
            log.info(MessageConstant.LOGIN_FAILED);
            throw new UserNotLoginException(MessageConstant.LOGIN_FAILED);
        }


        String password = userCreateDTO.getPassword();
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String password1 = encoder.encode(password);
        UserPO user = new UserPO();
        //属性拷贝
        BeanUtils.copyProperties(userCreateDTO,user);
        user.setPassword(password1);



        int insert = userMapper.insert(user);

        if (insert > 0) {
            // 获取刚插入用户的 ID
            Long userId = user.getId();

            return insert; // 返回用户插入结果
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertUserByGithub(UserPO user) {

        userMapper.insert(user);
    }



    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateUser(UserCreateDTO userDTO) {
        if (userDTO.getId() == null) {
            throw new IllegalArgumentException("用户ID不能为空");
        }

        // 查找现有用户
        UserPO user = userMapper.selectById(userDTO.getId());
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }

        // 更新字段，只更新非空字段
        if (StringUtils.hasText(userDTO.getUsername())) {
            user.setUsername(userDTO.getUsername());
        }
        if (StringUtils.hasText(userDTO.getPassword())) {
            // 如果密码不为空，则加密后更新
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            user.setPassword(encoder.encode(userDTO.getPassword()));
        }
        if (StringUtils.hasText(userDTO.getAvatar())) {
            user.setAvatar(userDTO.getAvatar());
        }
        if (StringUtils.hasText(userDTO.getEmail())) {
            user.setEmail(userDTO.getEmail());
        }
        if (StringUtils.hasText(userDTO.getPhone())) {
            user.setPhone(userDTO.getPhone());
        }

        // 执行更新
        return userMapper.updateById(user);
    }




    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserPO findByGithubId(String githubId) {
        if (githubId == null) {
            return null;
        }
        LambdaQueryWrapper<UserPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserPO::getGithubId, githubId).last("LIMIT 1");
        return this.getOne(queryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public UserVO getUserById(Long id) {



        // 用MyBatis-Plus查询
        UserPO user = userMapper.selectById(id);

        if (user == null) {
            return null;
        }

        // 将User拷贝到UserVO
        UserVO userVO = new UserVO();
        BeanUtils.copyProperties(user, userVO);
        return userVO;
    }

}
